fs = 16e3; % Known sample rate of the data set.

segmentDuration = 1;
frameDuration = 0.025;
hopDuration = 0.010;

FFTLength = 512;
numBands = 50;

segmentSamples = round(segmentDuration*fs);
frameSamples = round(frameDuration*fs);
hopSamples = round(hopDuration*fs);
overlapSamples = frameSamples - hopSamples;
afe = audioFeatureExtractor( ...
    SampleRate=fs, ...
    FFTLength=FFTLength, ...
    Window=hann(frameSamples,"periodic"), ...
    OverlapLength=overlapSamples, ...
    barkSpectrum=true);
setExtractorParameters(afe,"barkSpectrum",NumBands=numBands,WindowNormalization=false);

transform1 = transform(adsTrain,@(x)[zeros(floor((segmentSamples-size(x,1))/2),1);x;zeros(ceil((segmentSamples-size(x,1))/2),1)]);
transform2 = transform(transform1,@(x)extract(afe,x));
transform3 = transform(transform2,@(x){log10(x+1e-6)});
XTrain = readall(transform3,UseParallel=useParallel);
numFiles = numel(XTrain);
[numHops,numBands,numChannels] = size(XTrain{1});
XTrains = cat(4,XTrain{:});
[numHops,numBands,numChannels,numFiles] = size(XTrains);

transform1_v = transform(adsValidation,@(x)[zeros(floor((segmentSamples-size(x,1))/2),1);x;zeros(ceil((segmentSamples-size(x,1))/2),1)]);
transform2_v = transform(transform1_v,@(x)extract(afe,x));
transform3_v = transform(transform2_v,@(x){log10(x+1e-6)});
XValidation = readall(transform3_v,UseParallel=useParallel);
XValidations = cat(4,XValidation{:});
TTrain = adsTrain.Labels;
TValidation = adsValidation.Labels;

